local hx__aijianren = fk.CreateSkill {
  name = "hx__aijianren",
  tags = {Skill.Compulsory,},
}

hx__aijianren:addEffect(fk.DamageInflicted, {
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(hx__aijianren.name) and player.hp >= 1
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local x = data.damage
    if player.hp > 1 then
      data.damage = math.min(x, player.hp - 1)
    end
    if player.hp == 1 then
      data.damage = math.min(x, 1)
    end
  end,
})

hx__aijianren:addEffect(fk.EventPhaseEnd, {
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(hx__aijianren.name) and player.phase == Player.Discard then
      return #player.room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
        for _, move in ipairs(e.data) do
          if move.moveReason == fk.ReasonDiscard then
            for _, info in ipairs(move.moveInfo) do
              if table.contains(player.room.discard_pile, info.cardId) then
                return true
              end
            end
          end
        end
      end, Player.HistoryPhase) > 0
    end
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {
      skill_name = hx__aijianren.name,
      prompt = "是否将本阶段弃置的牌交给一名角色？",
    })
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local ids = {}
    room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
      for _, move in ipairs(e.data) do
        if move.moveReason == fk.ReasonDiscard then
          for _, info in ipairs(move.moveInfo) do
            if table.contains(room.discard_pile, info.cardId) then
              table.insertIfNeed(ids, info.cardId)
            end
          end
        end
      end
    end, Player.HistoryPhase)
    table.shuffle(ids)
    local to = room:askToChoosePlayers(player, {
      targets = room.alive_players,
      min_num = 1,
      max_num = 1,
      skill_name = hx__aijianren.name,
      prompt = "选择一名角色，将你本阶段弃置的牌交给其",
      cancelable = false,
    })
    room:moveCardTo(ids, Player.Hand, to[1], fk.ReasonGive, hx__aijianren.name, nil, true, player)
  end,
})



return hx__aijianren
